﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DLL;

namespace BLL
{
   public class KhachHangBLL
    {
       DataServer DB = new DataServer();
       public DataTable Select_All()
       {
           string sql = "select * from KhachHang";
           DataTable bang = DB.LayDulieu(sql);
           return bang;
       }
       public bool Them(KhachHangDAL kh)
       {
           string sql = string.Format("sp_KhachHang_Insert");
           string MaKH = DLL.CommonFunc.GetPrimaryKey("KhachHang");
           SqlParameter paramMa = new SqlParameter("@MaKH", MaKH);
           SqlParameter paramTen = new SqlParameter("@TenKH", kh.TenKH);
           SqlParameter paramDC = new SqlParameter("@DiaChi", kh.DiaChi);
           SqlParameter paramDT = new SqlParameter("@DienThoai", kh.DienThoai);
           SqlParameter paramEmail = new SqlParameter("@Mail", kh.EMail);
           try
           {
               DB.CapNhatProc(sql, paramMa, paramTen, paramDC, paramDT, paramEmail);
           }
           catch
           {
               return false;
           }
           DLL.CommonFunc.UpdatePrimaryKey("KhachHang");
           return true;
       }
       public bool Sua(KhachHangDAL kh)
       {
           string sql = string.Format("sp_KhachHang_Update");
           SqlParameter paramMa = new SqlParameter("@MaKH",kh.MaKH);
           SqlParameter paramTen = new SqlParameter("@TenKH", kh.TenKH);
           SqlParameter paramDC = new SqlParameter("@DiaChi", kh.DiaChi);
           SqlParameter paramDT = new SqlParameter("@DienThoai", kh.DienThoai);
           SqlParameter paramEmail = new SqlParameter("@Mail", kh.EMail);
           try
           {
               DB.CapNhatProc(sql, paramMa, paramTen, paramDC, paramDT, paramEmail);
           }
           catch
           {
               return false;
           }
           return true;
       }
       public bool Xoa(string Ma)
       {
           string sql = string.Format("sp_KhachHang_Delete");
           SqlParameter paramMa = new SqlParameter("@MaKH", Ma);
           try
           {
               DB.CapNhatProc(sql, paramMa);
           }
           catch
           {
               return false;
           }
           return true;
       }
       public DataTable Search(KhachHangDAL kh)
       {
           string sql = string.Format("sp_KhachHang_Search");
           SqlParameter paramMTB = new SqlParameter("@MaKH", kh.MaKH);
           SqlParameter paramTTB = new SqlParameter("@TenKH", kh.TenKH);
           DataTable bang = DB.LayDuLieuProc(sql, paramMTB, paramTTB);
           return bang;
       }
       public bool Kiemtra(string Ma)
       {
           string sql = string.Format("sp_KhachHang_Kiemtra");
           SqlParameter paramMNTB = new SqlParameter("@MaKH", Ma);
           DataTable bang = DB.LayDuLieuProc(sql, paramMNTB);
           if (bang.Rows.Count > 0)
           {
               return true;
           }
           return false;
       }
    }
}
